home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / orbits / passupdt / passupdt.pas < prev    next >
Pascal/Delphi Source File  |  1992-09-13  |  16KB  |  520 lines

  1. {$M 15520,0,0}
  2. Program Pass_Update_2;
  3. {           Author:  Dr TS Kelso }
  4. { Original Version:  1989 Nov 04 }
  5. { Current Revision:  1992 Sep 13 }
  6. {          Version:  2.50 }
  7. {        Copyright:  1989-1992, All Rights Reserved }
  8. {  Program Purpose:  Automatically updates two-line element set files. }
  9. {$N+}
  10.   Uses CRT,DOS,MinMax,SGP_Init,SGP_In,SGP_Time,Support;
  11.  
  12. const
  13.   tle_files = 49;
  14.  
  15. type
  16.   satnr    = string[5];
  17.   filename = string;
  18.  
  19. var
  20.   ask_to_update,ask_to_delete_old,
  21.   ask_to_delete_missing                   : boolean;
  22.   choice                                  : char;
  23.   lines,pause                             : word;
  24.   nr_files,position,old_position,
  25.   index,delta,counter                     : integer;
  26.   today                                   : double;
  27.   default_drive,old_elset,new_elset       : string[3];
  28.   old_satnr,new_satnr,sat_nr,obj_nr       : satnr;
  29.   satname                                 : string[25];
  30.   old_epoch,new_epoch                     : string[14];
  31.   outfilename,
  32.   update_filename,default_directory       : filename;
  33.   old_line,line                           : two_line;
  34.   tle_input,oldfile,tle_output,archive    : text;
  35.   selected                                : array [0..tle_files] of boolean;
  36.   tle_file                                : array [0..tle_files] of filename;
  37.   dirinfo                                 : SearchRec;
  38.  
  39. Procedure Initialize;
  40.   var
  41.     key         : char;
  42.     update_file : filename;
  43.     buffer      : string[80];
  44.     infile      : text;
  45.   begin
  46.   NormalVideo;
  47.   ClrScr;
  48.   Cursor_Off;
  49.   Writeln('╔════════════════════════╗');
  50.   Writeln('║  Two-Line Element Set  ║');
  51.   Writeln('║         Update         ║');
  52.   Writeln('╠════════════════════════╣');
  53.   Writeln('║       Written by       ║');
  54.   Writeln('║        TS Kelso        ║');
  55.   Writeln('║                        ║');
  56.   Writeln('║  Copyright 1989-1992   ║');
  57.   Writeln('║       All Rights       ║');
  58.   Writeln('║        Reserved        ║');
  59.   Writeln('╠════════════════════════╣');
  60.   Writeln('║      Version 2.50      ║');
  61.   Writeln('║      1992 Sep 13       ║');
  62.   Writeln('╚════════════════════════╝');
  63.   Writeln;
  64.   Writeln('This program will allow the user to automatically update all two-line element');
  65.   Writeln('set files (.TLE) in the default directory.  Within each file being updated,');
  66.   Writeln('element sets with a more recent epoch and element set number are automatic-');
  67.   Writeln('ally updated.  Otherwise, the user is queried as to whether an update should');
  68.   Writeln('be made.  The configuration can be set to bypass these queries.  The user also');
  69.   Writeln('has the option of updating archives of individual satellites from the master');
  70.   Writeln('file by specifying these satellites in a separate data file.');
  71.   Assign(infile,'PASSUPDT.CFG');
  72.   {$i-} Reset(infile); {$i+}
  73.   if IOResult <> 0 then
  74.     Report_Error(41,1,'File PASSUPDT.CFG missing!');
  75.   Readln(infile,default_drive);
  76.   if Pos(' ',default_drive) <> 0 then
  77.     default_drive := Copy(default_drive,1,Pos(' ',default_drive)-1);
  78.   Readln(infile,default_directory);
  79.   if Pos(' ',default_directory) <> 0 then
  80.     default_directory := Copy(default_directory,1,Pos(' ',default_directory)-1);
  81.   Readln(infile,update_file);
  82.   if Pos(' ',update_file) <> 0 then
  83.     update_file := Copy(update_file,1,Pos(' ',update_file)-1);
  84.   update_filename := update_file;
  85.   if Pos(':',update_filename) <> 0 then
  86.     Delete(update_filename,1,2);
  87.   if Pos('\',update_file) = 0 then
  88.     update_file := default_directory + update_file
  89.   else
  90.     while Pos('\',update_filename) <> 0 do
  91.       Delete(update_filename,1,Pos('\',update_filename));
  92.   if Pos(':',update_file) = 0 then
  93.     update_file := default_drive + update_file;
  94.   Readln(infile,pause);
  95.   Readln(infile,choice);
  96.   if Upcase(choice) = 'Y' then
  97.     ask_to_update := true
  98.   else
  99.     ask_to_update := false;
  100.   Readln(infile,choice);
  101.   if Upcase(choice) = 'Y' then
  102.     ask_to_delete_old := true
  103.   else
  104.     ask_to_delete_old := false;
  105.   Readln(infile,choice);
  106.   if Upcase(choice) = 'Y' then
  107.     ask_to_delete_missing := true
  108.   else
  109.     ask_to_delete_missing := false;
  110.   Readln(infile,lines);
  111.   if not (lines in [2,3]) then
  112.     lines := 3;
  113.   Close(infile);
  114.   Assign(tle_input,update_file);
  115.   {$i-} Reset(tle_input); {$i+}
  116.   if IOResult <> 0 then
  117.     Report_Error(41,1,'Master update file not found!');
  118.   counter := 0;
  119.   repeat
  120.     counter := counter + 1;
  121.     Readln(tle_input,buffer);
  122.   until Copy(buffer,1,2) = '1 ';
  123.   if lines = 3 then
  124.     counter := counter - 2
  125.   else
  126.     counter := counter - 1;
  127.   GotoXY(1,24);
  128.   Write('<Press any key to continue>');
  129.   repeat until keypressed;
  130.    key := ReadKey;
  131.   end; {Procedure Initialize}
  132.  
  133. Procedure Deinitialize;
  134.   begin
  135.   GotoXY(1,24);
  136.   ClrEOL;
  137.   NormalVideo;
  138.   Write('<Processing complete>');
  139.   Cursor_On;
  140.   end; {Procedure Deinitialize}
  141.  
  142. Procedure Epoch(var day : double);
  143.   var
  144.     yr,mo,dy,wd : word;
  145.   begin
  146.   GetDate(yr,mo,dy,wd);
  147.   day := Julian_Date_of_Year(yr) + DOY(yr,mo,dy);
  148.   end; {Procedure Epoch}
  149.  
  150. Function Set_Color(Line_1 : line_data) : word;
  151.   var
  152.     i     : integer;
  153.     edate : double;
  154.   begin
  155.   edate := Julian_Date_of_Epoch(Real_Value(line_1,19,14));
  156.   delta := Round(today - edate);
  157.   if delta < 0 then
  158.     Set_Color := LightBlue
  159.   else
  160.     case delta of
  161.        0..15 : Set_Color := LightGreen;
  162.       16..30 : Set_Color := Yellow;
  163.       31..45 : Set_Color := LightRed;
  164.     else
  165.       Set_Color := Brown;
  166.     end; {case}
  167.   end; {Function Set_Color}
  168.  
  169. Procedure Update_TLEs(TLE_filename : filename);
  170.   const
  171.     screen_pos = 15;
  172.   var
  173.     update,skip : boolean;
  174.     i           : integer;
  175.     checkfile   : text;
  176.   begin
  177.   skip := false;
  178.   for i := screen_pos to screen_pos + 9 do
  179.     begin
  180.     GotoXY(1,i);
  181.     ClrEOL;
  182.     end; {for i}
  183.   GotoXY(1,screen_pos);
  184.   TextColor(LightGray);
  185.   Write('Updating ',TLE_filename,'...');
  186.   Delay(pause);
  187.   Reset(tle_input);
  188.   for i := 1 to counter do
  189.     Readln(tle_input);
  190.   TLE_filename := Copy(TLE_filename,1,Pos('.',TLE_filename)-1);
  191.   Assign(checkfile,default_drive + default_directory + TLE_filename + '.BAK');
  192.   {$i-} Reset(checkfile); {$i+}
  193.   if IOResult = 0 then
  194.     begin
  195.     Close(checkfile);
  196.     Erase(checkfile);
  197.     end; {if}
  198.   Assign(oldfile,default_drive + default_directory + TLE_filename+'.TLE');
  199.   Rename(oldfile,default_drive + default_directory + TLE_filename+'.BAK');
  200.   Reset(oldfile);
  201.   Assign(tle_output,default_drive + default_directory + TLE_filename+'.TLE');
  202.   Rewrite(tle_output);
  203.   repeat
  204.     Readln(oldfile,satname);
  205.     Readln(oldfile,old_line[1]);
  206.     Readln(oldfile,old_line[2]);
  207.     GotoXY(1,screen_pos+2);
  208.     TextColor(White);
  209.     Writeln(satname);
  210.     TextColor(Set_Color(old_line[1]));
  211.     Writeln(old_line[1]);
  212.     Writeln(old_line[2]);
  213.     Writeln;
  214.     GotoXY(1,screen_pos+6);  ClrEOL;
  215.     GotoXY(1,screen_pos+7);  ClrEOL;
  216.     GotoXY(1,screen_pos+8);  ClrEOL;
  217.     GotoXY(1,screen_pos+9);  ClrEOL;
  218.     old_satnr := Copy(old_line[1],3,5);
  219.     old_epoch := Copy(old_line[1],19,14);
  220.     old_elset := Copy(old_line[1],66,3);
  221.     repeat
  222.       TextColor(LightGray);
  223.       if not skip then
  224.         begin
  225.         if lines = 3 then
  226.           Readln(tle_input);
  227.         Readln(tle_input,line[1]);
  228.         Readln(tle_input,line[2]);
  229.         new_satnr := Copy(line[1],3,5);
  230.         end; {if}
  231.       skip := false;
  232.       GotoXY(3,screen_pos+6);
  233.       Write(new_satnr);
  234.     until (new_satnr >= old_satnr) or EOF(tle_input);
  235.     if new_satnr <> old_satnr then
  236.       begin
  237.       GotoXY(1,screen_pos+6);
  238.       TextColor(LightRed);
  239.       Write('New data not found.  Delete? ');
  240.       Buzz;
  241.       if ask_to_delete_missing then
  242.         update := not yes
  243.       else
  244.         begin
  245.         update := true;
  246.         Write('No');
  247.         Delay(pause);
  248.         end; {else}
  249.       if update then
  250.         begin
  251.         Writeln(tle_output,satname);
  252.         Writeln(tle_output,old_line[1]);
  253.         Writeln(tle_output,old_line[2]);
  254.         end;
  255.       skip := true;
  256.       end {if}
  257.     else
  258.       begin
  259.       GotoXY(1,screen_pos+6);
  260.       TextColor(Set_Color(line[1]));
  261.       Writeln(line[1]);
  262.       Writeln(line[2]);
  263.       Writeln;
  264.       if Good_Elements(line) then
  265.         begin
  266.         new_epoch := Copy(line[1],19,14);
  267.         new_elset := Copy(line[1],66,3);
  268.         if (new_epoch >= old_epoch) and
  269.            (new_elset >  old_elset) then
  270.           begin
  271.           TextColor(LightGreen);
  272.           Write('Writing updated element set...');
  273.           Writeln(tle_output,satname);
  274.           Writeln(tle_output,line[1]);
  275.           Writeln(tle_output,line[2]);
  276.           end {if}
  277.         else
  278.           if (new_epoch = old_epoch) and
  279.              (new_elset = old_elset) then
  280.             begin
  281.             TextColor(Yellow);
  282.             Write('No change...');
  283.             if delta > 30 then
  284.               begin
  285.               Write('  Delete? ');
  286.               Buzz;
  287.               if ask_to_delete_old then
  288.                 update := not yes
  289.               else
  290.                 begin
  291.                 update := true;
  292.                 Write('No');
  293.                 Delay(pause);
  294.                 end; {else}
  295.               end {if}
  296.             else
  297.               update := true;
  298.             if update then
  299.               begin
  300.               Writeln(tle_output,satname);
  301.               Writeln(tle_output,old_line[1]);
  302.               Writeln(tle_output,old_line[2]);
  303.               end; {if}
  304.             end {if}
  305.           else
  306.             begin
  307.             Write('Replace? ');
  308.             Buzz;
  309.             if ask_to_update then
  310.               update := yes
  311.             else
  312.               if new_epoch > old_epoch then
  313.                 begin
  314.                 update := true;
  315.                 Write('Yes');
  316.                 Delay(pause);
  317.                 end {else if}
  318.               else
  319.                 begin
  320.                 update := false;
  321.                 Write('No');
  322.                 Delay(pause);
  323.                 end; {else else}
  324.             if update then
  325.               begin
  326.               Writeln(tle_output,satname);
  327.               Writeln(tle_output,line[1]);
  328.               Writeln(tle_output,line[2]);
  329.               end {if replace}
  330.             else
  331.               begin
  332.               Writeln(tle_output,satname);
  333.               Writeln(tle_output,old_line[1]);
  334.               Writeln(tle_output,old_line[2]);
  335.               end; {else}
  336.             end; {else}
  337.         end {if}
  338.       else
  339.         begin
  340.         TextColor(LightRed);
  341.         Writeln('Checksum(s) bad!');
  342.         Buzz; Buzz;
  343.         Writeln(tle_output,satname);
  344.         Writeln(tle_output,old_line[1]);
  345.         Writeln(tle_output,old_line[2]);
  346.         Delay(pause);
  347.         end; {else}
  348.       end; {else}
  349.     Delay(pause);
  350.   until EOF(oldfile);
  351.   Close(tle_input);
  352.   Close(oldfile);
  353.   Close(tle_output);
  354.   end; {Procedure Update_TLEs}
  355.  
  356. Procedure Show(index : byte);
  357.   begin
  358.   GotoXY(16*(index mod 5) + 1,(index div 5) + 3);
  359.   if index = position then
  360.     TextBackground(blue)
  361.   else
  362.     TextBackground(black);
  363.   if selected[index] then
  364.     TextColor(yellow)
  365.   else
  366.     TextColor(lightgray);
  367.   Write(Copy(' '+TLE_file[index]+'            ',1,14));
  368.   TextBackground(black);
  369.   end; {Procedure Show}
  370.  
  371. BEGIN
  372.  
  373.   Initialize;
  374.   Epoch(today);
  375.   FindFirst(default_drive+default_directory+'*.TLE',AnyFile,dirinfo);
  376.   If DOSError <> 0 then
  377.     Report_Error(41,1,'No *.TLE files found!');
  378.   ClrScr;
  379.   HighVideo;
  380.   Write('Select files to update:');
  381.   NormVideo;
  382.   Show_Status_Line('[A to toggle all, Cursor to position, <SPACE> to toggle, <CR> when done]');
  383.   index := -1;
  384.   position := 0;
  385.   while DOSError = 0 do
  386.     begin
  387.     index := index + 1;
  388.     TLE_file[index] := dirinfo.name;
  389.     selected[index] := false;
  390.     Show(index);
  391.     FindNext(dirinfo);
  392.     end; {while}
  393.   nr_files := index;
  394.   repeat
  395.     choice := Upcase(ReadKey);
  396.     case choice of
  397.       'A' : begin
  398.             for index := 0 to nr_files do
  399.               begin
  400.               selected[index] := not selected[index];
  401.               Show(index);
  402.               end; {for index}
  403.             end; {Toggle All}
  404.       'U' : begin
  405.             for index := 0 to nr_files do
  406.               begin
  407.               selected[index] := false;
  408.               Show(index);
  409.               end; {for index}
  410.             end; {Untag All}
  411.       ' ' : begin
  412.             selected[position] := not selected[position];
  413.             old_position := position;
  414.             position := IMin(position + 1,nr_files);
  415.             Show(old_position);
  416.             Show(position);
  417.             end; {Toggle}
  418.       #00 : begin
  419.             choice := ReadKey;
  420.             old_position := position;
  421.             case choice of
  422.               Home : position := 0;
  423.               Up   : position := IMax(position - 5,0);
  424.               Dn   : position := IMin(position + 5,nr_files);
  425.               Lt   : position := IMax(position - 1,0);
  426.               Rt   : position := IMin(position + 1,nr_files);
  427.               Endd : position := nr_files;
  428.               end; {case}
  429.             Show(old_position);
  430.             Show(position);
  431.             end; {Cursor positioning}
  432.       end; {case}
  433.   until choice = CR;
  434.   old_position := position;
  435.   GotoXY(1,25);
  436.   ClrEOL;
  437.   for index := 0 to nr_files do
  438.     if selected[index] and (tle_file[index] <> update_filename) then
  439.       begin
  440.       position := index;
  441.       Show(index);
  442.       Show(old_position);
  443.       GotoXY(1,1);
  444.       Update_TLEs(TLE_file[index]);
  445.       selected[index] := false;
  446.       old_position := position;
  447.       position := -1;
  448.       Show(index);
  449.       if keypressed then
  450.         if ReadKey = ESC then
  451.           Report_Error(41,1,'Processing interrupted...');
  452.       end; {if}
  453. { Update database files -- Dyy }
  454.   Assign(archive,'PASSUPDT.CAT');
  455.   {$i-} Reset(archive); {$i+}
  456.   if IOResult = 0 then
  457.     begin
  458.     ClrScr;
  459.     Writeln('Updating archive files...');
  460.     Write('[');  TextColor(lightgreen);
  461.     Write('Green');  TextColor(lightgray);
  462.     Writeln(' = Updated]');
  463.     Writeln;
  464.     Reset(tle_input);
  465.     for index := 1 to counter do
  466.       Readln(tle_input);
  467.     Readln(archive,sat_nr);
  468.     repeat
  469.       if lines = 3 then
  470.         Readln(tle_input);
  471.       Readln(tle_input,line[1]);
  472.       Readln(tle_input,line[2]);
  473.       obj_nr := Copy(line[1],3,5);
  474.       Write(obj_nr:8,^H^H^H^H^H^H^H^H);
  475.       if obj_nr >= sat_nr then
  476.         begin
  477.         if obj_nr = sat_nr then
  478.           begin
  479.           outfilename := 'SAT'+sat_nr+'.D'+Copy(line[1],19,2);
  480.           Assign(tle_output,default_drive + default_directory + outfilename);
  481.           {$i-} Reset(tle_output); {$i+}
  482.           if IOResult = 0 then
  483.             begin
  484.             repeat
  485.               Readln(tle_output,old_line[1]);
  486.               Readln(tle_output,old_line[2]);
  487.             until EOF(tle_output);
  488.             if (old_line[1] <> line[1]) or (old_line[2] <> line[2]) then
  489.               begin
  490.               Append(tle_output);
  491.               TextColor(lightgreen);
  492.               Write(sat_nr:8);
  493.               TextColor(lightgray);
  494.               Writeln(tle_output,line[1]);
  495.               Writeln(tle_output,line[2]);
  496.               end {if}
  497.             else
  498.               Write(sat_nr:8);
  499.             end {if}
  500.           else
  501.             begin
  502.             Rewrite(tle_output);
  503.             TextColor(lightgreen);
  504.               Write(sat_nr:8);
  505.             TextColor(lightgray);
  506.             Writeln(tle_output,line[1]);
  507.             Writeln(tle_output,line[2]);
  508.             end; {else}
  509.           Close(tle_output);
  510.           end; {if}
  511.         if not EOF(archive) then
  512.           Readln(archive,sat_nr);
  513.         end; {if}
  514.     until EOF(tle_input);
  515.     ClrEOL;
  516.     end; {if}
  517.   DeInitialize;
  518.  
  519. END.
  520.